<%@page import="com.mysql.cj.xdevapi.JsonArray"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.Array"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ page import="java.io.IOException" %>
<%@page
import="java.io.PrintWriter"
import ="java.net.URLEncoder"
import=" java.sql.Connection"
import ="java.sql.DriverManager"
import ="java.sql.PreparedStatement"
import ="java.sql.ResultSet"
import ="javax.servlet.ServletException"
import=" javax.servlet.annotation.WebServlet"
import ="javax.servlet.http.HttpServlet"
import ="javax.servlet.http.HttpServletRequest"
import=" javax.servlet.http.HttpServletResponse"
import=" javax.swing.JOptionPane" %>

<%@ page import="org.json.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>调配</title>

<%

JSONArray pointls=new JSONArray();

try {
	request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
	Class.forName("com.mysql.cj.jdbc.Driver");
	Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/yj?autoReconnect=true&serverTimezone=GMT","root","root");	
	String x=new String();
	x=request.getParameter("goods");
	if (x.equals("帐篷")){
	PreparedStatement p=con.prepareStatement("SELECT ck.*,goods.* FROM ck,goods WHERE ck.ckid=goods.ckid&& count1 >0");ResultSet res=p.executeQuery();
	int i=0;
	while(res.next()) {	
		JSONObject point=new JSONObject();	
		point.put("wd", res.getFloat("ck.site_w"));
		point.put("jd", res.getFloat("ck.site_j"));
		
		point.put("qyid", res.getInt("qyid"));
		
		point.put("z_jd", request.getParameter("jd"))	;
		point.put("z_wd", request.getParameter("wd"))	;
		point.put("ckid", res.getInt("ckid"))	;
		
	pointls.put(i, point);
	i++;
}
	request.setAttribute("pointls", pointls);
	
	
	con.close();p.close();		}
	
	if (x.equals("食品")){PreparedStatement p=con.prepareStatement("SELECT ck.*,goods.* FROM ck,goods WHERE ck.ckid=goods.ckid&& count2 >0");ResultSet res=p.executeQuery();
	int i=0;
	while(res.next()) {	
		JSONObject point=new JSONObject();	
		point.put("wd", res.getFloat("ck.site_w"));
		point.put("jd", res.getFloat("ck.site_j"));
		
		point.put("qyid", res.getInt("qyid"));
		
		point.put("z_jd", request.getParameter("jd"))	;
		point.put("z_wd", request.getParameter("wd"))	;
		point.put("ckid", res.getInt("ckid"))	;
		
	pointls.put(i, point);
	i++;
}
	request.setAttribute("pointls", pointls);
	
	
	con.close();p.close();		}

	if (x.equals("医疗物资"))
	{PreparedStatement p=con.prepareStatement("SELECT ck.*,goods.* FROM ck,goods WHERE ck.ckid=goods.ckid&& count3 >0");ResultSet res=p.executeQuery();
	int i=0;
	while(res.next()) {	
		JSONObject point=new JSONObject();	
		point.put("wd", res.getFloat("ck.site_w"));
		point.put("jd", res.getFloat("ck.site_j"));
		
		point.put("qyid", res.getInt("qyid"));
		
		point.put("z_jd", request.getParameter("jd"))	;
		point.put("z_wd", request.getParameter("wd"))	;
		point.put("ckid", res.getInt("ckid"))	;
		
	pointls.put(i, point);
	i++;
}
	request.setAttribute("pointls", pointls);
	
	
	con.close();p.close();		}
	if (x.equals("水"))
	{PreparedStatement p=con.prepareStatement("SELECT ck.*,goods.* FROM ck,goods WHERE ck.ckid=goods.ckid&& count4 >0");ResultSet res=p.executeQuery();
	int i=0;
	while(res.next()) {	
		JSONObject point=new JSONObject();	
		point.put("wd", res.getFloat("ck.site_w"));
		point.put("jd", res.getFloat("ck.site_j"));
		
		point.put("qyid", res.getInt("qyid"));
		
		point.put("z_jd", request.getParameter("jd"))	;
		point.put("z_wd", request.getParameter("wd"))	;
		point.put("ckid", res.getInt("ckid"))	;
		
	pointls.put(i, point);
	i++;
}
	request.setAttribute("pointls", pointls);
	
	
	con.close();p.close();		}
	if (x.equals("器械"))
	{PreparedStatement p=con.prepareStatement("SELECT ck.*,goods.* FROM ck,goods WHERE ck.ckid=goods.ckid&& count5>0");ResultSet res=p.executeQuery();
	int i=0;
	while(res.next()) {	
		JSONObject point=new JSONObject();	
		point.put("wd", res.getFloat("ck.site_w"));
		point.put("jd", res.getFloat("ck.site_j"));
		
		point.put("qyid", res.getInt("qyid"));
		
		point.put("z_jd", request.getParameter("jd"))	;
		point.put("z_wd", request.getParameter("wd"))	;
		point.put("ckid", res.getInt("ckid"))	;
		
	pointls.put(i, point);
	i++;
}
	request.setAttribute("pointls", pointls);
	
	
	con.close();p.close();		}
	
	
		
		
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}   
%>

<style type="text/css">
html,body{
    width:100%;
    height:100%;
}
#container{
    width:100%;
    height:95%;
}
*{
    margin:0px;
    padding:0px;
}
body, button, input, select, textarea {
    font: 12px/16px Verdana, Helvetica, Arial, sans-serif;
}
</style>

<script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=XYLBZ-IYFC6-UWMS5-MWU4V-TAKO3-4NFH5&&libraries=drawing,geometry,autocomplete,convertor"></script>


</head>
<body onload="init();">

<input type = 'hidden' id = 'test' name = 'test' value = '<%=pointls.toString() %>'></input>
<input type = 'hidden' id = 'zqid' name = 'zqid' value = '<%=request.getParameter("id") %>'></input>
<div id="container"></div>

<button onclick="zui();">最短路程</button>
<button   onclick="init();">返回</button>
</body>

<script>
var distance=new  Array();
function zui(){
	var center = new qq.maps.LatLng(39.916527,116.397128);
    var map = new qq.maps.Map(document.getElementById("container"), {
        center: center,
        zoom: 4
    });
    var infoWin = new qq.maps.InfoWindow({
        map: map
    });
    
    var zqid = document.getElementById("zqid").value;
    var latlngs = new Array();
	
    	var x = document.getElementById("test").value;
    	
    	pointls = JSON.parse(x);
    	
    	
    	for(var i=0;i<pointls.length;i++)
    		{
    			latlngs.push(new qq.maps.LatLng(pointls[i].wd,pointls[i].jd));
    		}
    	 var anchor = new qq.maps.Point(6, 6),
         size = new qq.maps.Size(24, 24),
         origin = new qq.maps.Point(0, 0),
    	icon = new qq.maps.MarkerImage('https://mapapi.qq.com/web/lbs/javascriptV2/demo/img/center.gif', size, origin, anchor);
    	 var w=pointls[1].z_wd;var e=pointls[1].z_jd;
    	var zq= new qq.maps.LatLng(w,e);
    	var marker2 = new qq.maps.Marker({
    		icon: icon,
            position:zq ,
            map: map
        });
    	
   var min=0;
   for(var i=1;i<distance.length;i++){
	   
   var p=distance[0];
 
   if(p>distance[i])
   {min=i;
   }
   }
   var marker = new qq.maps.Marker({
       position: latlngs[min],
       map: map
   });
   qq.maps.event.addListener(marker, 'click', function() {
       infoWin.open();
       infoWin.setContent('<div style="text-align:center;white-space:'+
       'nowrap;margin:10px;">仓库id: ' +
       pointls[min].ckid+ ' <br>企业ID: ' +
       pointls[min].qyid+'<br><button ><a href="diaodu.jsp?ckid='+pointls[min].ckid+'&zqid='+zqid+'">物资调配</a></button></div>');
       infoWin.setPosition(latlngs[min]);
   });
   
   
 var map,markersArray = [],route_lines=[];
   function showPoly(pointList) { 
		for (var i = 0; i < pointList.length; i++) {
		   var start =i;var end= i+1;
	       if(!pointList[end])return;
	       var driving =new qq.maps.DrivingService({
	            complete : function(response){               
	               directions_routes = response.detail.routes;
	               //所有可选路线方案
	               for(var i = 0;i < directions_routes.length; i++){
	                    var route = directions_routes[i];
	                    map.fitBounds(response.detail.bounds); //调整地图窗口显示所有路线 
	                    console.log(route.distance);
	                    var polyline = new qq.maps.Polyline(
	                        {
	                            path: route.path,
	                            strokeColor: '#3893F9',
	                            strokeWeight: 6,
	                            map: map
	                        }
	                    );route_lines.push(polyline);
	               }
	            }
	       
	       
	       
	        });
			driving.search(pointList[start], pointList[end]); //waypoints表示途经点
		}
	}
   
   var porints=[
	   {"name":"坐标3","xy":[e,w]},
	   //{"name":"坐标1","xy":[114.484815,38.025003]},
	   //{"name":"坐标2","xy":[114.521142,38.041544]},
	   {"name":"坐标4","xy":[pointls[min].jd,pointls[min].wd]}
	   ];
   var arrayList = [];
   for (var i in porints) {
   	var p = porints[i].xy;
   	var p1 =new qq.maps.LatLng(p[1],p[0]);
   	arrayList.push(p1);    
       
   }
   showPoly(arrayList);//显示轨迹
   
   
   }
   
   
function init() {	
    var center = new qq.maps.LatLng(39.916527,116.397128);
    var map = new qq.maps.Map(document.getElementById("container"), {
        center: center,
        zoom: 4
    });
    var infoWin = new qq.maps.InfoWindow({
        map: map
    });
    
  
    var latlngs = new Array();
	
    	var x= document.getElementById("test").value;
    	
    	pointls = JSON.parse(x);
    	
    	
    	for(var i=0;i<pointls.length;i++)
    		{
    			latlngs.push(new qq.maps.LatLng(pointls[i].wd,pointls[i].jd));
    		}
    	 var anchor = new qq.maps.Point(6, 6),
         size = new qq.maps.Size(24, 24),
         origin = new qq.maps.Point(0, 0),
    	icon = new qq.maps.MarkerImage('https://mapapi.qq.com/web/lbs/javascriptV2/demo/img/center.gif', size, origin, anchor);
    	 var w=pointls[1].z_wd;var e=pointls[1].z_jd;
    	var marker2 = new qq.maps.Marker({
    		icon: icon,
            position:new qq.maps.LatLng(w,e) ,
            map: map
        });
    
    for(var i = 0;i < latlngs.length; i++) {
    	(function(n){
            var marker = new qq.maps.Marker({
                position: latlngs[n],
                map: map
            });
            
            qq.maps.event.addListener(marker, 'click', function() {
                infoWin.open();
                infoWin.setContent('<div style="text-align:center;white-space:'+
                'nowrap;margin:10px;">仓库id: ' +
                pointls[n].ckid+ ' <br>企业ID: ' +
                pointls[n].qyid+'<br><button ><a href="qygoods2.jsp?id='+pointls[n].ckid+'">查看仓库物资</a></button></div>');
                infoWin.setPosition(latlngs[n]);
            });
        })(i);
    }
    
 var map,markersArray = [],route_lines=[]; 
    function showPoly(pointList) { 
 		for (var i = 1; i < pointList.length; i++) {
 			var start= 0; var end= i;
 	       if(!pointList[end])return;
 	       var driving =new qq.maps.DrivingService({
 	            complete : function(response){               
 	               directions_routes = response.detail.routes;
 	               //所有可选路线方案
 	              
 	               for(var i = 0;i < directions_routes.length; i++){
 	                    var route = directions_routes[i];
 	                    map.fitBounds(response.detail.bounds); //调整地图窗口显示所有路线 
 	                    
 	                    var polyline = new qq.maps.Polyline(
 	                        {
 	                            path: route.path,
 	                            strokeColor: '#3893F9',
 	                            strokeWeight: 6,
 	                            map: map
 	                        }
 	                    );route_lines.push(polyline);
 	                    distance.push(route.distance);
 	               }
 	            }
 	       
 	       
 	        });
 			driving.search(pointList[start], pointList[end]); //waypoints表示途经点
 		}
 	}
    
    
    var arrayList = [];
    arrayList.push(new qq.maps.LatLng(w,e));
    for (var i in pointls) {
    	
    	var p1 =new qq.maps.LatLng(pointls[i].wd,pointls[i].jd);
    	arrayList.push(p1);    
        
    }
    showPoly(arrayList);//显示轨迹
    
    console.log(distance)
    
}
</script>


</html>